package com.yangcangclub.commons.mail;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.servlet.ServletContext;

import com.yangcangclub.commons.config.SysConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

/**
 * 
 * 邮件服务信息配置
 * 	实现InitializingBean 初始化
 * 
 *
 */
public class MailConfig implements InitializingBean, ServletContextAware {

	/** WEB服务器域名 */
	private String serverHost;

	/** WEB服务器名称 */
	private String serverName = "unknown";

	/** 邮件配置路径 */
	private String mailConfigPath;

	/** 邮件服务的配置信息 */
	private Properties mailProperties;

	/** 邮件服务主机地址 */
	private String host;

	/** 邮件服务端口 */
	private int port = 0;

	/** 邮件用户名 */
	private String userName;

	/** 邮件用户的密码 */
	private String password;

	/** 系统名称 */
	private String systemName;

	/** 异常发生时,邮件服务器往此邮件地址发送邮件 */
	private String execeptionEmail;

	private boolean isEnable;

	/** Servlet上下文 */
	private ServletContext context;

	@Autowired
	private SysConfig sysConfig;

	/**
	 * 初始化,如果WEB服务器以"localhost"设置本机地址，则自动取得WEB服务主机的IP
	 */
	public void afterPropertiesSet() throws Exception {
		//初始化邮件配置
		this.initMailConfig();
		//初始化WEB服务器域地址/名
		if ("localhost".equalsIgnoreCase(serverHost)) {
			try {
				InetAddress localAddress = InetAddress.getLocalHost();
				this.serverHost = localAddress.getHostAddress();
				this.serverName = localAddress.getHostName();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 注入Servlet上下文
	 */
	public void setServletContext(ServletContext servletContext) {
		this.context = servletContext;
	}
	
	/**
	 * 初始化邮件配置
	 */
	private void initMailConfig() {
		this.setEnable(sysConfig.isMailIsEnable());
		this.setHost(sysConfig.getMailServerHost());
		this.setPassword(sysConfig.getMailServerPassword());
		if (sysConfig.getMailServerPort() != null) {
			this.setPort(Integer.valueOf(sysConfig.getMailServerPort()).intValue());
		}
		if (sysConfig.getMailServerSystemName() != null) {
			this.setSystemName(sysConfig.getMailServerSystemName());
		}
		if (sysConfig.getMailServerExeceptionEmail() != null) {
			this.setExeceptionEmail(sysConfig.getMailServerExeceptionEmail());
		}
		this.setUserName(sysConfig.getMailServerUserName());
		if (sysConfig.getMailServerHost() != null) {
			this.setServerHost(sysConfig.getMailServerHost());
		}
	}


	/* setters and getters */
	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getMailConfigPath() {
		return mailConfigPath;
	}

	public void setMailConfigPath(String mailConfigPath) {
		this.mailConfigPath = mailConfigPath;
	}

	public Properties getMailProperties() {
		return mailProperties;
	}

	public void setMailProperties(Properties mailProperties) {
		this.mailProperties = mailProperties;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSystemName() {
		return systemName;
	}

	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}

	public String getExeceptionEmail() {
		return execeptionEmail;
	}

	public void setExeceptionEmail(String execeptionEmail) {
		this.execeptionEmail = execeptionEmail;
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public SysConfig getSysConfig() {
		return sysConfig;
	}

	public void setSysConfig(SysConfig sysConfig) {
		this.sysConfig = sysConfig;
	}

	public boolean isEnable() {
		return isEnable;
	}

	public void setEnable(boolean isEnable) {
		this.isEnable = isEnable;
	}
}
